<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 68KB
 *
 * An open source knowledge base script
 *
 * @package		68kb
 * @author		68kb Dev Team
 * @copyright	Copyright (c) 2009, 68 Designs, LLC
 * @license		http://68kb.com/user_guide/license.html
 * @link		http://68kb.com
 * @since		Version 1.0
 */

// ------------------------------------------------------------------------

/**
 * Users Model
 *
 * Handles users
 *
 * @package		68kb
 * @subpackage	Models
 * @category	Models
 * @author		68kb Dev Team
 * @link		http://68kb.com/user_guide/overview/users.html
 * @version 	$Id: users_model.php 172 2009-12-11 16:18:32Z suzkaw68 $
 */
class Users_model extends model
{	
	function __construct()
	{
		parent::__construct();
		log_message('debug', 'Users Model Initialized');
	}
	
	// ------------------------------------------------------------------------
		
	/**
	* Add user
	* 
	* @param	int
	* @return	int on success.
	*/
	function add_user($data)
	{
		$this->db->insert('users', $data);
		if ($this->db->affected_rows() > 0) 
		{
			$id = $this->db->insert_id();
			$this->core_events->trigger('users/add', $id);
			return $id;
		}
		return FALSE;
	}
	
	// ------------------------------------------------------------------------
		
	/**
	* Delete user
	* 
	* @param	int
	* @return	TRUE on success.
	*/
	function delete_user($id)
	{
		$id = (int) $id;
		$this->db->delete('users', array('id' => $id)); 
		if ($this->db->affected_rows() > 0) 
		{
			return TRUE;
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
 	* Edit User
 	* 
	* @param	int
 	* @param	array
 	* @return	TRUE on success.
 	*/
	function edit_user($user_id, $data)
	{
		if (isset($data['password']) && $data['password'] != '')
		{
			$data['password'] = md5($data['password']);
		}
		
		$user_id = (int) $user_id;
		$this->db->where('id', $user_id);
		$this->db->update('users', $data);
		
		if ($this->db->affected_rows() > 0) 
		{
			return TRUE;
		}
		else
		{
			log_message('info', 'Could not edit the user id '. $user_id);
			return FALSE;
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Get User By ID.
	 *
	 * Get a single user by their ID
	 *
	 * @access	public
	 * @param	int the id
	 * @return	array
	 */
	function get_user_by_id($id)
	{
		$this->db->from('users')->where('id', $id);
		$query = $this->db->get();
		$data = $query->row();
		$query->free_result();
		return $data;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Get Users
	 *
	 * Get all the settings from the db.
	 *
	 * @access	public
	 * 
	 * @return	array
	 */
	function get_users()
	{
		$this->db->from('users');
		$query = $this->db->get();
		return $query;
	}	
}

/* End of file users_model.php */
/* Location: ./upload/includes/application/models/users_model.php */ 